<?php

namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;

class Base extends Controller
{
    //thinkphp5 tp5
    public $expiration_time = 1200;
    public function _initialize()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,hj-token");
        header('Access-Control-Allow-Methods: GET, POST');
//        $head = $this->request = Request::instance()->header();
//        if(!isset($head['token'])){
//            $this->apiReturn(303,'token不存在');
//        }
//        $token = $head['token'];
//        $value = Db::name('user')->field('id,expiration_time')->where(['token'=>$token])->find();
//        if($value){
//            if(time() > $value['expiration_time']){
//                $this->apiReturn(303,'token已经过期，请从新登录');
//            }else{
//                Db::name('user')->update(['id'=>$value['id'],'expiration_time'=>time()+1200]);
//            }
//        }else{
//            $this->apiReturn(303,'token值不存在，非法获取');
//        }
    }

    public function apiReturn( $code=200 , $msg='', $data = array() ,$total=0 ){
        $returnData = [
            'code' => $code,
            'msg'  => $msg?$msg:return_msg($code),
            'data' => $data,
            'total'=>$total,
        ];

        if(I('callback')){
            echo I('callback').'('.json_encode($returnData).')';die;
        }
        //$returnData[1] = decodeUnicode($returnData[1]);
        echo json_encode($returnData);die;
    }


}